<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
<!--            <div class="layui-form toolbar">-->
<!--                <div class="layui-form-item">-->
<!--                    <div class="layui-inline">-->
<!--&lt;!&ndash;                        <button id="cellSluggishMaterialsBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&ndash;&gt;-->
<!--&lt;!&ndash;                        <button id="cellSluggishMaterialsBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>&ndash;&gt;-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
            <!-- 表格 -->
            <table class="layui-table" id="cellSluggishMaterialsTable" lay-filter="cellSluggishMaterialsTable"></table>
        </div>
    </div>
</div>
<style>
    #cellSluggishMaterialsTable{
        height: 0;
    }
    div[lay-id='cellSluggishMaterialsTable'] .layui-table-tool{
        position: initial;
        text-align: right;
        width: 100%;
    }
</style>

<!-- 表格操作列 -->
<script type="text/html" id="cellSluggishMaterialsTableBar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="cellSluggishMaterialsForm">
    <form lay-filter="cellSluggishMaterialsForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">创建者</label>
            <div class="layui-input-block">
                <input name="creator" placeholder="请输入创建者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改者</label>
            <div class="layui-input-block">
                <input name="modifier" placeholder="请输入修改者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-block">
                <input name="creatorName" placeholder="请输入创建人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-block">
                <input name="modifierName" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物料编号</label>
            <div class="layui-input-block">
                <input name="skuCode" placeholder="请输入物料编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">企业编码</label>
            <div class="layui-input-block">
                <input name="enterpriseCode" placeholder="请输入企业编码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="cellSluggishMaterialsFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        let currentTime = new Date();

        // 渲染表格
        var insTb = table.render({
            elem: '#cellSluggishMaterialsTable',
            url: config.base + 'enterprisecab/cabinet/admin/cellSluggishMaterials/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'}
                ,{field: 'skuName', sort: false, title: '物料名称'}
                ,{field: 'skuCode', sort: false, title: '物料编号'}
                ,{field: 'oldSpecCode', sort: false, title: '规格码', templet: function (d) {
                        return d.oldSpecCode ? d.oldSpecCode : "无"
                    }}
                ,{field: 'productBrandName', sort: false, title: '品牌', templet: function (d) {
                        return d.productBrandName ? d.productBrandName : "无"
                    }}
                ,{field: 'quantityPack', sort: true, title: '数量(包装计)', width: 150}
                ,{field: 'unitName', sort: true, title: '包装单位', templet: d=> d.unitName ? d.unitName : '无'}
                ,{field: 'quantity', sort: true, title: '数量(基础单位计)', width: 150}
                ,{field: 'basicsUnitName', sort: true, title: '基础单位', templet: d=> d.basicsUnitName ? d.basicsUnitName : '无'}
                ,{field: 'exhibitTime', sort: false, title: '上货时间'}
                ,{field: 'lethargyThreshold', sort: false, title: '呆滞时长阈值'}
                ,{field: 'receiptorTime', sort: false, title: '最后领取时间'}
                ,{field: '', sort: false, title: '呆滞时长',templet:d => {
                     const targetDate = new Date(d.receiptorTime);
                     const diffInMs = currentTime.getTime() - targetDate.getTime();
                     const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24));
                     const diffInHours = Math.floor((diffInMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                     return `${diffInDays}天${diffInHours}小时`
                    }}
                // ,{align: 'center', toolbar: '#cellSluggishMaterialsTableBar', title: '操作', width: 200}
            ]]
        });

        // 工具条点击事件
        table.on('tool(cellSluggishMaterialsTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(cellSluggishMaterialsTable)', function(obj){
            table.reload('cellSluggishMaterialsTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#cellSluggishMaterialsBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '660px',
                offset: '65px',
                title: data ? '修改呆滞物料管理' : '添加呆滞物料管理',
                content: $('#cellSluggishMaterialsForm').html(),
                success: function () {
                    form.val('cellSluggishMaterialsForm', data);
                    // 表单提交事件
                    form.on('submit(cellSluggishMaterialsFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprisecab/cabinet/admin/cellSluggishMaterials/update' : 'enterprisecab/cabinet/admin/cellSluggishMaterials/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('cellSluggishMaterialsTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/cabinet/admin/cellSluggishMaterials/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#cellSluggishMaterialsBtnExport').click(function () {
            var checkRows = table.checkStatus('cellSluggishMaterialsTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>